package com.itheima.api;

import com.itheima.Type.CommentType;
import com.itheima.mongo.Comment;
import com.itheima.mongo.Movement;
import com.itheima.vo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


//跨城调用服务
@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    //发布评论 并获取评论的数量
    public Integer save(Comment comment) {
        //查询动态 通过前端传过来的动态id 查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //向comment 圈子互动表 对象置被评论人属性
        if (movement != null) {
            //获取发布动态用户的信息 通过动态id查询 该动态谁发布的
            comment.setPublishUserId(movement.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(comment);
        //更新动态表中的对应字段 通过动态id 获取该动态字段
        Query query = Query.query(Criteria.where("id")
                .is(comment.getPublishId()));
        //创建更新对象
        Update update = new Update();
        //判断操作类类型    点赞
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            //点赞加1
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            //评论加1
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        //去数据库 更具被评论人id 查询动态  更新互动类型 获得更新后的最新数据 通过动态表查询
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType());
    }

    //删除
    @Override
    public Integer delete(Comment comment) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType() );
    }

    //分页查询
    public List<Comment> findComments(String movementId, CommentType commentType, Integer page, Integer pagesize) {
        //1、构造查询条件
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType")
                        .is(commentType.getType()))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //2、查询并返回
        return mongoTemplate.find(query, Comment.class);
    }


    /**
     * 判断comment数据是否存在
     *查询用户是否点赞该动态的方法
     */
    //判断comment数据是否存在
    public Boolean hasComment(String movementId, Long userId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class); //判断数据是否存在
    }

    //判断该评论是否存在
    @Override
    public Boolean commentsThumb(String movementId, Long userId, CommentType like) {
        Criteria criteria=Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(like.getType());
        Query query=Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class);
    }


    //发布评论 并获取评论的数量
    @Override
    public Integer save1(Comment comment) {
        //查询动态 通过前端传过来的动态id 查询动态
        Comment pinglun = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        //向comment 圈子互动表 对象置被评论人属性
        if (pinglun != null) {
            //获取发布动态用户的信息 通过动态id查询 该动态谁发布的
            comment.setPublishUserId(pinglun.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(comment);
        //更新动态表中的对应字段 通过动态id 获取该动态字段
        Query query = Query.query(Criteria.where("id")
                .is(comment.getPublishId()));

        //创建更新对象
        Update update = new Update();
        //判断操作类类型    点赞
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            //点赞加1
            update.inc("likeCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        //去数据库 更具被评论人id 查询动态  更新互动类型 获得更新后的最新数据 通过动态表查询
        Comment zuixin= mongoTemplate.findAndModify(query, update, options, Comment.class);
        //获取最新的评论数量，并返回
        return zuixin.statisCount(comment.getCommentType());

    }

    /**
     *取消评论点赞
     */
    @Override
    public Integer delete2(Comment comment) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、修改动态表中的总数量
        Query commentQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Comment comment1 = mongoTemplate.findAndModify(commentQuery, update, options, Comment.class);
        //5、获取最新的评论数量，并返回
        return comment1.statisCount(comment.getCommentType() );
    }

}
